package ru.cybertek.struts2.action;

import ru.cybertek.dao.PersonDao;
import ru.cybertek.domain.Person;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Dmitry Demyankov
 * @version $Revision: 10 $
 *  
 *          (C) Copyright 2009 CyberTek Ltd. All rights reserved.
 */
public class PersonManagementAction extends ActionSupport
{
    public static final String MODE_EDIT = "edit";
    
    private static final long serialVersionUID = 3711158965719304590L;

    private String id;

    private String firstName;

    private String middleName;

    private String lastName;

    private String mode;
    
    private PersonDao personDao;

    /**
     * @return
     * @throws Exception
     */
    public String list() throws Exception
    {
        return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String add() throws Exception
    {
        Person person = new Person();
        person.setFirstName(firstName);
        person.setMiddleName(middleName);
        person.setLastName(lastName);
        
        if ((id != null) & !id.equals(""))
        	person.setId(Integer.valueOf(id));

        personDao.savePerson(person);
        clearFieldValues();

        return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String edit() throws Exception
    {
        Person person = personDao.getPerson(Integer.valueOf(id));
        
        id = person.getId().toString();
        firstName = person.getFirstName();
        middleName = person.getMiddleName();
        lastName = person.getLastName();
        
        mode = MODE_EDIT;
        
        return SUCCESS;
    }
    
    /**
     * @return
     * @throws Exception
     */
    public String delete() throws Exception
    {
        Person person = new Person();
        person.setId(Integer.valueOf(id));
        
        personDao.deletePerson(person);
        clearFieldValues();
        
        return SUCCESS;
    }
    
    /**
     * @return
     * @throws Exception
     */
    public String cancel() throws Exception
    {
        clearFieldValues();
        
        return SUCCESS;
    }
    
    /**
     * Clears field values.
     */
    private void clearFieldValues()
    {
        id = null;
        firstName = null;
        middleName = null;
        lastName = null;
        mode = null;
    }
    
    /**
     * Getter for property id.
     * 
     * @return the id
     */
    public String getId()
    {
        return id;
    }

    /**
     * Setter for property id.
     * 
     * @param id
     *            the id to set
     */
    public void setId(String id)
    {
        this.id = id;
    }

    /**
     * Getter for property firstName.
     * 
     * @return the firstName
     */
    public String getFirstName()
    {
        return firstName;
    }

    /**
     * Setter for property firstName.
     * 
     * @param firstName
     *            the firstName to set
     */
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    /**
     * Getter for property middleName.
     * 
     * @return the middleName
     */
    public String getMiddleName()
    {
        return middleName;
    }

    /**
     * Setter for property middleName.
     * 
     * @param middleName
     *            the middleName to set
     */
    public void setMiddleName(String middleName)
    {
        this.middleName = middleName;
    }

    /**
     * Getter for property lastName.
     * 
     * @return the lastName
     */
    public String getLastName()
    {
        return lastName;
    }

    /**
     * Setter for property lastName.
     * 
     * @param lastName
     *            the lastName to set
     */
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    /**
     * Getter for property mode.
     *
     * @return the mode
     */
    public String getMode()
    {
        return mode;
    }

    /**
     * Setter for property mode.
     *
     * @param mode the mode to set
     */
    public void setMode(String mode)
    {
        this.mode = mode;
    }

    /**
     * Getter for property personDao.
     * 
     * @return the personDao
     */
    public PersonDao getPersonDao()
    {
        return personDao;
    }

    /**
     * Setter for property personDao.
     * 
     * @param personDao
     *            the personDao to set
     */
    public void setPersonDao(PersonDao personDao)
    {
        this.personDao = personDao;
    }
}
